package develop.iforce.jtest;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.c2dm.C2DMessaging;

public class JschaActivity extends Activity {
    public static Handler mH;

    private MediaPlayer mMediaPlayer;
    public Toast toastText;
    public Vibrator vibrator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	requestWindowFeature(Window.FEATURE_NO_TITLE);

        // C2DMサーバーへ登録
        C2DMessaging.register(this, "iforce.develop@gmail.com");

        mH = new Handler() {
            public void handleMessage(android.os.Message msg) {
                if (msg.getData().getBoolean("receivedMessageFlag")) {

                	String type = msg.getData().getString("receivedTypeString");
                	String message = msg.getData().getString("receivedMessageString");
                	String level = msg.getData().getString("receivedLevelString");
                	String sec = msg.getData().getString("receivedSecString");
                	String tsunami = msg.getData().getString("receivedTsunamiString");

                    //ToastにViewを設定して表示
                	toastText = new Toast(JschaActivity.this);
                    LayoutInflater inflater = getLayoutInflater();
                    if(type.equals("message")){
                    	View v = inflater.inflate(R.layout.message, null);

                        TextView textView = (TextView) v.findViewById(R.id.messagetext);
                        textView.setText(message);

                        //端末通知音
                        mMediaPlayer = MediaPlayer.create(JschaActivity.this, Settings.System.DEFAULT_NOTIFICATION_URI);

                        toastText.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL, 0, 0);
                        toastText.setView(v);
                    }else if(type.equals("earthquake")){
                    	View v = inflater.inflate(R.layout.earthquake, null);

                    	TextView textView = (TextView) v.findViewById(R.id.earthtext);
                        textView.setText("地震が発生しました。強い揺れに警戒してください。\n予想震度：" + level + "   到達予想時間：" + sec +"   津波："+tsunami);

                        //地震警報音
                        mMediaPlayer = MediaPlayer.create(JschaActivity.this, R.raw.jishin);

                        toastText.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
                        toastText.setView(v);
                    }
                    mMediaPlayer.start();

                    vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                    vibrator.vibrate(300);

                    toastText.setDuration(Toast.LENGTH_LONG);
                    toastText.show();
                }
            }
        };
        finish();
    }

}